/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.HashMap;
import javax.swing.JOptionPane;
import model.dominio.Usuario;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

/**
 *
 * @author Rodrigo
 */
public class ControllerRelatorio {

    private static Connection con;

    public static Connection getConexao() {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/locarrao_rent_a_car";
        String login = "root";
        String senha = "123456";
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, login, senha);
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, "Driver não encontrado: " + e.getMessage());
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Erro na conexão: " + e.getMessage());
        }
        return con;
    }
    public static JasperPrint relatorioVeiculosRentabilidade() {
        JasperPrint rel = null;
        try {
            getConexao();
            HashMap map = new HashMap();
            String arquivoJasper = "relatorios/veiculo_rentabilidade.jasper";
            rel = JasperFillManager.fillReport(arquivoJasper, map, con);
        } catch (JRException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        return rel;
    }
    public static JasperPrint relatorioVeiculosLocacoes() {
        JasperPrint rel = null;
        try {
            getConexao();
            HashMap map = new HashMap();
            String arquivoJasper = "relatorios/veiculo_locacoes.jasper";
            rel = JasperFillManager.fillReport(arquivoJasper, map, con);
        } catch (JRException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        return rel;
    }
    public static JasperPrint relatorioLocacoes(String data1, String data2) {
        JasperPrint rel = null;
        try {
            getConexao();
            HashMap map = new HashMap();
            if(!data1.equals("  /  /    ")){
                Calendar c = (Calendar) Calendar.getInstance().clone();
                String[] aux=data1.split("/");
                c.set(Integer.parseInt(aux[2]),Integer.parseInt(aux[1]),Integer.parseInt(aux[0]));
                map.put("DATA1",c.getTime());
            }
            
            if(!data2.equals("  /  /    ")){
                Calendar c = (Calendar) Calendar.getInstance().clone();
                String[] aux=data2.split("/");
                c.set(Integer.parseInt(aux[2]),Integer.parseInt(aux[1]),Integer.parseInt(aux[0]));
                map.put("DATA2",c.getTime());
            }
            String arquivoJasper = "relatorios/locacoes_fechadas.jasper";
            rel = JasperFillManager.fillReport(arquivoJasper, map, con);
        } catch (JRException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        return rel;
    }
    public static JasperPrint relatorioLog(String data1, String data2, Usuario user) {
        JasperPrint rel = null;
        try {
            getConexao();
            HashMap map = new HashMap();
            if(!data1.equals("  /  /    ")){
                Calendar c = (Calendar) Calendar.getInstance().clone();
                String[] aux=data1.split("/");
                c.set(Integer.parseInt(aux[2]),Integer.parseInt(aux[1]),Integer.parseInt(aux[0]));
                map.put("DATA1",c.getTime());
            }
            
            if(!data2.equals("  /  /    ")){
                Calendar c = (Calendar) Calendar.getInstance().clone();
                String[] aux=data2.split("/");
                c.set(Integer.parseInt(aux[2]),Integer.parseInt(aux[1]),Integer.parseInt(aux[0]));
                map.put("DATA2",c.getTime());
            }
            map.put("USER", user.getCodigo());
            String arquivoJasper = "relatorios/acoes_usuario.jasper";
            rel = JasperFillManager.fillReport(arquivoJasper, map, con);
        } catch (JRException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        return rel;
    }

}
